<?php

namespace App\Http\Requests\Admin\User\User;

use App\Models\User;
use App\Rules\MobileRule;
use App\Rules\NickNameRule;
use App\Rules\PasswordRule;
use Illuminate\Foundation\Http\FormRequest;

class CreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
            'mobile'=>['required',new MobileRule,function($attribute, $value, $fail){
                $where = [];
                $where['mobile']=$value;
                $user = User::where($where)->count('id');
                if(!$user) {
                    return true;
                } else {
                    $fail( ':attribute已存在.');
                }
            },'bail'],
            'nickname'=>['required',new NickNameRule,'bail'],
            'password'=>['required',new PasswordRule,'confirmed','bail'],//password_confirmation
            'status'=>'required|boolean|bail'
        ];
    }
    public function attributes()
    {
        return [
            'nickname'=>'昵称',
            'password'=>'密码',
            'password_confirmation'=>'确认密码',
            'mobile'=>'手机号',
            'status'=>'状态'
        ];
    }
}
